class Solution {
 public:
  ListNode* removeNthFromEnd(ListNode* head, int n) {
    ListNode* dum = new ListNode(0, head);
    ListNode* first = head;
    ListNode* second = dum;
    for (int i = 0; i < n; ++i) {
      first = first->next;
    }
    while (first) {
      first = first->next;
      second = second->next;
    }
    second->next = second->next->next;
    head = dum->next;
    delete dum;
    return head;
  }
};

